--[[                      -*- Mode: lua; lua-indent-level: 2; tab-width: 2 -*-
Baggins_Outfitter

Author: Daphne Pfister <tiiroib02@sneakemail.com>

Thanks to Nargiddley for writing Baggins and John Stephen and Bruce Quinton for writing ItemRack

License

Copyright (c) 2008 Daphne Pfister

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

-- Did some variable renaming to make the code slightly closer to the coding style
-- used by Outfitter... to allow this code to be included in that addon if the author wishes.

--[[
if not AceLibrary then return end
if not Baggins then return end
]]

Baggins_Outfitter = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0","AceHook-2.1")

local Baggins_Outfitter = Baggins_Outfitter

local dewdrop = AceLibrary:HasInstance("Dewdrop-2.0") and AceLibrary("Dewdrop-2.0")
local L = AceLibrary("AceLocale-2.2"):new("Baggins_Outfitter")
local gSetsTable = {}

L:RegisterTranslations("enUs", function()
	return {
		["Outfitter Set:%s"] = true,
		["Unknown"] = true,
		["Outfitter Sets"] = true,
		["Set Name"] = true,
		["Any"] = true,
		["None"] = true,
		["Self"] = true,
		["Outfitter"] = true,
		["Filter by Outfitter set"] = true,
		["Match by the item number, ignoring enchantments, variations, armor kits, etc."] = true,
	};
end)

-- You have my permission to fix any of these locales without
-- asking me and check them into the SVN.
L:RegisterTranslations("esES", function()
	return {
		--["Outfitter Set:%s"] = true,
		--["Unknown"] = true,
		--["Outfitter Sets"] = true,
		--["Set Name"] = true,
		--["Any"] = true,
		["None"] = "Ninguno", -- check
		--["Self"] = true,
		--["Outfitter"] = true,
		--["Filter by Outfitter set"] = true,
		--["Match by the item number, ignoring enchantments, variations, armor kits, etc."] = true,
	};
end)

L:RegisterTranslations("deDE", function()
	return {
		--["Outfitter Set:%s"] = true,
		["Unknown"] = "Unbekannt", -- check
		--["Outfitter Sets"] = true,
		--["Set Name"] = true,
		--["Any"] = true,
		["None"] = "Keine",
		--["Self"] = true,
		--["Outfitter"] = true,
		--["Filter by Outfitter set"] = true,
		--["Match by the item number, ignoring enchantments, variations, armor kits, etc."] = true,
	};
end)

L:RegisterTranslations("frFR", function()
	return {
		--["Outfitter Set:%s"] = true,
		["Unknown"] = "Inconnu", -- check
		--["Outfitter Sets"] = true,
		--["Set Name"] = true,
		--["Any"] = true,
		["None"] = "Aucun", -- check
		--["Self"] = true,
		--["Outfitter"] = true,
		--["Filter by Outfitter set"] = true,
		--["Match by the item number, ignoring enchantments, variations, armor kits, etc."] = true,
	};
end)

L:RegisterTranslations("koKR", function()
	return {
		["Outfitter Set:%s"] = "Outfitter 세트명:%s",
		["Unknown"] = "가격미확인", -- check
		["Outfitter Sets"] = "Outfitter 세트",
		["Set Name"] = "세트명",
		["Any"] = "모두",
		["None"] = "없음", -- check
		["Self"] = "자신",
		--["Outfitter"] = true,
		["Filter by Outfitter set"] = "Outfitter 세트에 따라 필터링",
		["Match by the item number, ignoring enchantments, variations, armor kits, etc."] = "마법부여나 키트 효과등을 무시하고 아이템 ID에 따라 분류합니다",
	};
end)

L:RegisterTranslations("znCN", function()
	return {
		--["Outfitter Set:%s"] = true,
		["Unknown"] = "未知", -- check
		--["Outfitter Sets"] = true,
		--["Set Name"] = true,
		--["Any"] = true,
		["None"] = "无", -- check
		--["Self"] = true,
		--["Outfitter"] = true,
		--["Filter by Outfitter set"] = true,
		--["Match by the item number, ignoring enchantments, variations, armor kits, etc."] = true,
	};
end)

L:RegisterTranslations("znTW", function()
	return {
		--["Outfitter Set:%s"] = true,
		["Unknown"] = "未知", -- check
		--["Outfitter Sets"] = true,
		--["Set Name"] = true,
		--["Any"] = true,
		["None"] = "無", -- check
		--["Self"] = true,
		--["Outfitter"] = true,
		--["Filter by Outfitter set"] = true,
		--["Match by the item number, ignoring enchantments, variations, armor kits, etc."] = true,
	};
end)

-- Prototypes
local Matches -- (pBag, pSlot, pRule)
local GetName -- (pRule)
local CleanRule -- (pRule)
local SetRuleSet -- (pRule, pSet)
local DewDropOptions -- (pRule, pLevel, pValue)
local CacheSets -- ()

-- Events
local Initialize
local Terminate
local AddOutfit
local DeleteOutfit
local EditOutfit


local function Matches(pBagIndex, pSlotIndex, pRule)
	local vItemInfo = Outfitter:GetBagItemInfo(pBagIndex, pSlotIndex)
	if not vItemInfo then return end

	local vSet = pRule.set
	if not vSet then
		if pRule.test == "All" then
			-- Support rules from Thorias's version of Baggins_Outfitter
			pRule.set = "Any"
			vSet = "Any"
		end
		if not vSet then return end
	end

	if vSet == "None" then
		for vName,vOutfit in pairs(gSetsTable) do
			if Outfitter:OutfitUsesItem(vOutfit, vItemInfo) then return nil end
		end
		return true
  elseif vSet == "Any" then
		for vName,vOutfit in pairs(gSetsTable) do
			if Outfitter:OutfitUsesItem(vOutfit, vItemInfo) then return true end
		end
		return nil
	end

	local vOutfit = gSetsTable[vSet]
	if not vOutfit then return nil end
	return Outfitter:OutfitUsesItem(vOutfit, vItemInfo)
end

local function GetName(pRule)
	return L["Outfitter Set:%s"]:format(pRule.set or L["Unknown"])
end

local function CleanRule(pRule)
	pRule.set = "Any"
	pRule.test = "All" -- Support rules from Thorias's version of Baggins_Outfitter
	Baggins:OnRuleChanged()
end

local function SetRuleSet(pRule, pSet)
	-- Support rules from Thorias's version of Baggins_Outfitter
	pRule.set = pSet
	if pSet == "Any" then
		pRule.test = "All"
	else
		pRule.test = nil
	end
	Baggins:OnRuleChanged()
end

local function DewDropOptions(pRule, pLevel, pValue)
	-- Support rules from Thorias's version of Baggins_Outfitter
	if not pRule.set then
		if pRule.test == "All" then
			pRule.set = "Any"
		end
	end

	dewdrop:AddLine('text', L["Outfitter Sets"], 'isTitle', true)
	dewdrop:AddLine("text", L["Any"], "checked", pRule.set == "Any", 'func',SetRuleSet,'arg1',pRule,'arg2',"Any")
	dewdrop:AddLine("text", L["None"], "checked", pRule.set == "None", 'func',SetRuleSet,'arg1',pRule,'arg2',"None")
	local setnames = {}
	for set in pairs(gSetsTable) do
		table.insert(setnames,set)
	end
	table.sort(setnames)
	for _,set in ipairs(setnames) do
		dewdrop:AddLine('text', set, "checked", pRule.set == set, 'func',SetRuleSet,'arg1',pRule,'arg2',set)
	end
end

local function CacheSets()
	gSetsTable = {}
	if not Outfitter.Initialized then return end

	local vCategoryOrder = Outfitter:GetCategoryOrder()
	for vCategoryIndex, vCategoryID in ipairs(vCategoryOrder) do
		local vOutfits = Outfitter:GetOutfitsByCategoryID(vCategoryID)

		for vIndex, vOutfit in ipairs(vOutfits) do
			if vOutfit and vOutfit.Name then
				gSetsTable[vOutfit.Name] = vOutfit
			end
		end
	end
end

function Baggins_Outfitter:OnInitialize()
	CacheSets()
	Baggins:AddCustomRule("Outfitter", {
		DisplayName = L["Outfitter"],
		Description = L["Filter by Outfitter set"],
		Matches = Matches,
		GetName = GetName,
		CleanRule = CleanRule,
		DewDropOptions = DewDropOptions
	})
end

local function Initialize()
	CacheSets()
	Baggins:ForceFullUpdate()
end

local function Terminate()
	gSetsTable = {}
end

local function AddOutfit()
	CacheSets()
	Baggins:ForceFullUpdate()
end

local function DeleteOutfit()
	CacheSets()
	Baggins:ForceFullUpdate()
end

local function EditOutfit()
	Baggins:ForceFullUpdate()
end

function Baggins_Outfitter:OnEnable()
	--CacheSets()

	if not Outfitter.Initialized then
		Outfitter:RegisterOutfitEvent("INITIALIZE",Initialize)
	end
	Outfitter:RegisterOutfitEvent("TERMINATE",Terminate)
	Outfitter:RegisterOutfitEvent("ADD_OUTFIT",AddOutfit)
	Outfitter:RegisterOutfitEvent("DELETE_OUTFIT",DeleteOutfit)
	Outfitter:RegisterOutfitEvent("EDIT_OUTFIT",EditOutfit)
end

function BOSets() return gSetsTable end
function BOCache() return CacheSets() end
